package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("AddressBookController")
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址簿")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 查询地址簿
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<AddressBook> addressBookList = addressBookService.getByUserId();
        return Result.success(addressBookList);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result updateDefault(@RequestBody AddressBook addressBook){
        addressBookService.updateDefaultAddress(addressBook);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam Long id){
        addressBookService.deleteById(id);
        return Result.success();
    }
}
